package com.ihr360.payroll.model.dto.salary.basecode;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.commons.model.dto.BaseCode;
import com.ihr360.commons.model.type.NumOperation;
import com.ihr360.payroll.model.vo.SalaryFieldCategoryVo;
import com.ihr360.payroll.type.FieldTaxType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import java.util.ArrayList;
import java.util.List;

/**
 * 薪酬字段类型
 * <p>
 * 工资
 * 奖金
 * 补贴/津贴
 * 补偿金
 * 扣款
 * 社保
 * 公积金
 * 个人所得税
 * 报销
 * 外籍员工税后报销
 * 其他成本项
 * 考勤信息
 * 统计项
 * 股息红利
 * 其他收入
 */
@Entity
@Cache(region = "cnb.base", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SalaryFieldType extends BaseCode<Long> {
    private static final long serialVersionUID = 5989168001312597485L;
    public static final String TOTAL = "total";



    //按照最新分类定义的编码
    public static final class Code {
        //工资
        public static final String salary = "salary";
        //奖金
        public static final String bonus = "bonus";
        //税前津补贴
        public static final String pretaxallowance = "pretaxallowance";
        //税前扣款
        public static final String pretaxdeduction = "pretaxdeduction";
        //应税调整
        public static final String taxableadjustment = "taxableadjustment";
        //劳务费
        public static final String laborincome = "laborincome";
        //年终奖
        public static final String yearendbonus = "yearendbonus";
        //补偿金
        public static final String compensation = "compensation";
        //股息红利
        public static final String dividends = "dividends";
        //其它收入
        public static final String otherincome = "otherincome";
        //社保公积金个人
        public static final String personsihf = "personsihf";
        //社保公积金单位
        public static final String companysihf = "companysihf";
        //税费
        public static final String tax = "tax";
        //税后报销
        public static final String aftertaxexpenses = "aftertaxexpenses";
        //外籍税后报销
        public static final String foreignaftertaxexpenses = "foreignaftertaxexpenses";
        //税后扣款
        public static final String aftertaxdection = "aftertaxdection";
        //其它成本
        public static final String othercost = "othercost";
        //考勤
        public static final String attendance = "attendance";
        //统计
        public static final String statistics = "statistics";
        //过渡项目
        public static final String Transient = "transient";
        //其它
        public static final String other = "other";

        //员工
        public static final String staff = "staff";

    }


    /**
     * 薪酬字段分类(大类)
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    private SalaryFieldCategory category;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name="salary_field_tax_type",
            joinColumns=@JoinColumn(name="salary_field_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="salary_tax_type_id", referencedColumnName="id"))
    private List<SalaryTaxType> salaryTaxTypes = new ArrayList<>();

    /**
     * 是否允许不计税
     */
    private boolean salaryTaxTypeRequired = true;

    private boolean customizable = true;

    /**
     * 序号
     */
    @Column(unique = false)
    private int indexno = 1;

    @Column(length = 20)
    @Enumerated(EnumType.STRING)
    private NumOperation companycostop;

    @Column(length = 20)
    @Enumerated(EnumType.STRING)
    private NumOperation personalsalaryop;

    /**
     *
     * 计税类型 税前加项
     */
    @Column(length = 20)
    @Enumerated(EnumType.STRING)
    private FieldTaxType fieldTaxType;

    @Column(columnDefinition = "bit(1) default 0")
    private boolean embedded;

    public FieldTaxType getFieldTaxType() {
        return fieldTaxType;
    }

    public void setFieldTaxType(FieldTaxType fieldTaxType) {
        this.fieldTaxType = fieldTaxType;
    }

    public NumOperation getCompanycostop() {
        return companycostop;
    }

    public void setCompanycostop(NumOperation companycostop) {
        this.companycostop = companycostop;
    }

    public NumOperation getPersonalsalaryop() {
        return personalsalaryop;
    }

    public void setPersonalsalaryop(NumOperation personalsalaryop) {
        this.personalsalaryop = personalsalaryop;
    }

    /*
         * 是否生成分类汇总 项目
         */
    private boolean genTypeTotalField = false;



    public SalaryFieldCategory getCategory() {
        return category;
    }

    public void setCategory(SalaryFieldCategory category) {
        this.category = category;
    }

    public int getIndexno() {
        return indexno;
    }

    public void setIndexno(int indexno) {
        this.indexno = indexno;
    }

    public List<SalaryTaxType> getSalaryTaxTypes() {
        return salaryTaxTypes;
    }

    public void setSalaryTaxTypes(List<SalaryTaxType> salaryTaxTypes) {
        this.salaryTaxTypes = salaryTaxTypes;
    }

    public boolean isSalaryTaxTypeRequired() {
        return salaryTaxTypeRequired;
    }

    public void setSalaryTaxTypeRequired(boolean salaryTaxTypeRequired) {
        this.salaryTaxTypeRequired = salaryTaxTypeRequired;
    }

    public boolean isCustomizable() {
        return customizable;
    }

    public void setCustomizable(boolean customizable) {
        this.customizable = customizable;
    }

    public boolean isGenTypeTotalField() {
        return genTypeTotalField;
    }

    public void setGenTypeTotalField(boolean genTypeTotalField) {
        this.genTypeTotalField = genTypeTotalField;
    }


    public String totalCode() {
        return this.code.concat(TOTAL);
    }

    @JsonIgnore
    public SalaryFieldCategoryVo getSalaryTaxTypeVO() {
        if (category == null) {
            return null;
        }
        return new SalaryFieldCategoryVo(category.getId(),category.getCode(), category.getName());
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}